package com.chestnut.system.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chestnut.common.log.annotation.Log;
import com.chestnut.common.log.enums.BusinessType;
import com.chestnut.common.security.web.PageRequest;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.contentcore.domain.CmsPageRich;
import com.chestnut.contentcore.domain.CmsQuestionAnswers;
import com.chestnut.contentcore.service.ICmsQuestionAnswersService;
import com.chestnut.system.security.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chestnut.common.security.web.BaseRestController;
import com.chestnut.common.security.anno.Priv;
import com.chestnut.system.security.AdminUserType;
import lombok.RequiredArgsConstructor;
import com.chestnut.common.domain.R;

/**
 * 问题答案Controller
 *
 * @author chestnut
 * @date 2025-04-27
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/answers")
public class CmsQuestionAnswersController extends BaseRestController {

    @Autowired
    private ICmsQuestionAnswersService cmsQuestionAnswersService;

    /**
     * 查询问题答案列表
     */
//    @Priv
    @GetMapping("/list")
    public R<?> list(CmsQuestionAnswers cmsQuestionAnswers)
    {
        PageRequest pr = this.getPageRequest();
        LambdaQueryChainWrapper<CmsQuestionAnswers> wrapper = this.cmsQuestionAnswersService.lambdaQuery();
        if (cmsQuestionAnswers.getQuestion() != null) {
            wrapper.like(CmsQuestionAnswers::getQuestion,cmsQuestionAnswers.getQuestion());
        }
        if (cmsQuestionAnswers.getAnswers() != null) {
            wrapper.like(CmsQuestionAnswers::getAnswers,cmsQuestionAnswers.getAnswers());
        }
        if (cmsQuestionAnswers.getLanguage() != null) {
            wrapper.like(CmsQuestionAnswers::getLanguage,cmsQuestionAnswers.getLanguage());
        }
        Page<CmsQuestionAnswers> page = wrapper
                .page(new Page<>(pr.getPageNumber(), pr.getPageSize(), true));

        return this.bindDataTable(page);
    }

    /**
     * 获取问题答案详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:answers:query')")
    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(this.cmsQuestionAnswersService.getById(id));
    }

    /**
     * 新增问题答案
     */
//    @Priv
    @Log(title = "问题答案", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody CmsQuestionAnswers cmsQuestionAnswers)
    {
        cmsQuestionAnswers.setCreateTime(LocalDateTime.now());
        cmsQuestionAnswers.createBy(StpAdminUtil.getLoginUser().getUsername());
        this.cmsQuestionAnswersService.save(cmsQuestionAnswers);
        return R.ok();
    }

    /**
     * 修改问题答案
     */
//    @Priv
    @Log(title = "问题答案", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody CmsQuestionAnswers cmsQuestionAnswers)
    {
        this.cmsQuestionAnswersService.updateById(cmsQuestionAnswers);
        return R.ok();
    }

    /**
     * 删除问题答案
     */
//    @Priv
    @Log(title = "问题答案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        this.cmsQuestionAnswersService.removeBatchByIds(List.of(ids));
        return R.ok();
    }
}
